package com.card.admin.servelt;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONObject;

import com.card.admin.pojo.Admin;
import com.card.admin.pojo.AdminManager;
import com.card.admin.pojo.SellerLog;
import com.card.log.LogType;
import com.card.log.LogUtils;
import com.card.match.MatchManager;
import com.card.match.MatchSetting;
import com.card.notice.GlobNotice;
import com.card.notice.NoticeManager;
import com.card.setting.GameSetting;
import com.card.user.StringParam;
import com.card.user.User;
import com.card.user.UserCurrency.CurrencyKind;
import com.card.user.UserUtils;
import com.xiaoao.net.utils.PubFunc;
import com.xiaoao.net.utils.StringUtil;
import com.xiaoao.net.utils.TimeUtils;

public class DoActionServletPhone extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		res.setContentType("text/plain;charset=utf-8");
		req.setCharacterEncoding("utf-8");
		String action = req.getParameter("action");
		System.out.println(action);

		if ("wx".equals(action)) {
			String gm = req.getParameter("goumai");
			String dl = req.getParameter("daili");

			String andoridxz = req.getParameter("andoridxz");
			String iosxz = req.getParameter("iosxz");
			String gongGao = req.getParameter("gongGao");
			int code = PubFunc.parseInt(req.getParameter("ykdl"));
			int initGold = PubFunc.parseInt(req.getParameter("initGold"));
			int minCode = PubFunc.parseInt(req.getParameter("minCode"));
			int iosMinCode = PubFunc.parseInt(req.getParameter("iosMinCode"));
			int ExchangeScale = PubFunc.parseInt(req.getParameter("ExchangeScale"));

			if (!StringUtil.NullOrEmpty(gongGao)) {
				GameSetting.getInstance().setGongGao(gongGao);
			}
			if (!StringUtil.NullOrEmpty(gm)) {
				GameSetting.getInstance().setWxBuy(gm);
			}
			if (!StringUtil.NullOrEmpty(dl)) {
				GameSetting.getInstance().setWxDl(dl);
			}
			if (!StringUtil.NullOrEmpty(andoridxz)) {
				GameSetting.getInstance().setAndroidXiazai(andoridxz);
			}
			if (!StringUtil.NullOrEmpty(iosxz)) {
				GameSetting.getInstance().setIosXiazai(iosxz);
			}
			GameSetting.getInstance().setTouristCode(code);
			GameSetting.getInstance().setInitGold(initGold);
			GameSetting.getInstance().setMinCode(minCode);
			GameSetting.getInstance().setIosMinCode(iosMinCode);
			GameSetting.getInstance().saveWx();
			req.getRequestDispatcher("/m_wx.jsp").forward(req, res);
			return;
		} else if ("share".equals(action)) {
			int shareGold1 = PubFunc.parseInt(req.getParameter("share1"));
			int shareGold2 = PubFunc.parseInt(req.getParameter("share2"));

			int share1Gold = PubFunc.parseInt(req.getParameter("share1Gold"));
			int shareDayMaxGold = PubFunc.parseInt(req.getParameter("shareDayMaxGold"));
			int binCard = PubFunc.parseInt(req.getParameter("binCard"));

			GameSetting.getInstance().setShareGold1(shareGold1);
			GameSetting.getInstance().setShareGold2(shareGold2);
			GameSetting.getInstance().setShare1Gold(share1Gold);
			GameSetting.getInstance().setShareDayMaxGold(shareDayMaxGold);
			GameSetting.getInstance().setBinCard(binCard);

			GameSetting.getInstance().saveWx();
			req.getRequestDispatcher("/m_share.jsp").forward(req, res);
			return;
		} else if ("fangka".equals(action)) {
			int mj1 = PubFunc.parseInt(req.getParameter("mj1"));
			int mj2 = PubFunc.parseInt(req.getParameter("mj2"));
			int ddz1 = PubFunc.parseInt(req.getParameter("ddz1"));
			int ddz2 = PubFunc.parseInt(req.getParameter("ddz2"));
			int zjh1 = PubFunc.parseInt(req.getParameter("zjh1"));
			int zjh2 = PubFunc.parseInt(req.getParameter("zjh2"));
			int zjh3 = PubFunc.parseInt(req.getParameter("zjh3"));
			int nn1 = PubFunc.parseInt(req.getParameter("nn1"));
			int nn2 = PubFunc.parseInt(req.getParameter("nn2"));
			int nn3 = PubFunc.parseInt(req.getParameter("nn3"));
			int pdk1 = PubFunc.parseInt(req.getParameter("pdk1"));
			int pdk2 = PubFunc.parseInt(req.getParameter("pdk2"));
			int[] mj = GameSetting.getInstance().getMjModeGolds();
			int[] ddz = GameSetting.getInstance().getDdzModeGolds();
			int[] zjh = GameSetting.getInstance().getZjhModeGolds();
			int[] nn = GameSetting.getInstance().getNnModeGolds();
			int[] pdk = GameSetting.getInstance().getPdkModeGolds();
			mj[0] = mj1;
			mj[1] = mj2;
			ddz[0] = ddz1;
			ddz[1] = ddz2;
			zjh[0] = zjh1;
			zjh[1] = zjh2;
			zjh[2] = zjh3;
			nn[0] = nn1;
			nn[1] = nn2;
			nn[2] = nn3;
			pdk[0] = pdk1;
			pdk[1] = pdk2;
			GameSetting.getInstance().saveWx();
			req.getRequestDispatcher("/m_fangka.jsp").forward(req, res);
			return;
		} else if ("resetpwd".equals(action)) {
			String userName = req.getParameter("username");
			String password = req.getParameter("password");
			String newpwd = req.getParameter("newpwd");
			String newpwd2 = req.getParameter("newpwd2");
			if (!StringUtil.equals(newpwd, newpwd2)) {
				req.getRequestDispatcher("/m_info.jsp?info=1").forward(req, res);
				return;
			}
			Admin admin = AdminManager.getInstance().getAdmin(userName);
			if (admin != null && StringUtil.equals(password, admin.getPassword()) && admin.isFree()) {
				admin.setPassword(newpwd);
				AdminManager.getInstance().changePwd(admin.getId() + "", newpwd);
				req.getRequestDispatcher("/m_index.jsp").forward(req, res);
				return;
			}
			req.getRequestDispatcher("/m_info.jsp?info=2").forward(req, res);
			return;
		/*} else if ("roomsetting".equals(action)) {
			String jushu = req.getParameter("jushu");
			String renshu = req.getParameter("renshu");
			int hongzhong = "on".equals(req.getParameter("hongzhong")) ? 1 : 0;
			int zhuang = "on".equals(req.getParameter("zhuang")) ? 1 : 0;
			int nofeng = "on".equals(req.getParameter("nofeng")) ? 1 : 0;
			String maima = req.getParameter("maima");
			String name = req.getParameter("name");

			String roonsetting = jushu + "," + renshu + "," + hongzhong + "," + zhuang 
					+ "," + nofeng + "," + maima+ "," + name;

			System.out.println("roomse" + jushu + renshu + hongzhong + zhuang + nofeng + maima);

			Admin admin = (Admin) req.getSession().getAttribute("admin");
			if (admin == null) {
				return;
			}
			// System.out.println(admin.getName());
			AdminManager.getInstance().changRoomSetting("" + admin.getId(), roonsetting);
			req.getRequestDispatcher("/m_roomsetting.jsp").forward(req, res);
			return;*/
		} else if ("addagent".equals(action)) {
			String userName = req.getParameter("username");
			String password = req.getParameter("password");
			String phone = req.getParameter("phone");
			User user = UserUtils.getUserByPrimaryKey(userName);
			if (user == null) {
				req.getRequestDispatcher("/m_info.jsp?info=6").forward(req, res);
				return;
			}
			Admin admin = AdminManager.getInstance().getAdmin(userName);
			if (admin != null) {
				req.getRequestDispatcher("/m_info.jsp?info=3").forward(req, res);
				return;
			}
			HttpSession session = req.getSession();
			Admin fatherAdmin = (Admin) session.getAttribute("admin");
			int fatherId = 1;
			if (fatherAdmin != null) {
				fatherId = fatherAdmin.getId();
			}
			AdminManager.getInstance().addAgent(fatherId, userName, password, phone);
			req.getRequestDispatcher("/m_agent.jsp").forward(req, res);
			return;
		} else if ("czagent".equals(action)) {
			String id = req.getParameter("id");
			int addGold = PubFunc.parseInt(req.getParameter("gold"));
			HttpSession session = req.getSession();
			Admin admin = (Admin) session.getAttribute("admin");

			if (admin == null) {
				return;
			}
			if (addGold <= 0) {
				req.getRequestDispatcher("/m_info.jsp?info=5").forward(req, res);
				return;
			}

			admin = AdminManager.getInstance().getAdminById(admin.getId() + "");
			Admin admin1 = AdminManager.getInstance().getAdminById(id);
			User user = UserUtils.getUserByPrimaryKey(admin1.getName());
			if (admin.getGroup() == StringParam.Group_Agent) {
				if (addGold < 0 || admin.getGold() < addGold) {
					req.getRequestDispatcher("/m_info.jsp?info=5").forward(req, res);
					return;
				}
				User user1 = UserUtils.getUserByPrimaryKey(admin.getName());
				System.out.println(user1.getFatherId());
				AdminManager.getInstance().addGold(admin, -addGold,id);
				user1.userCurrency().add(CurrencyKind.Diamonds, -addGold);
				LogUtils.addSpendLog(user1, LogType.GMFK.getDesc() + admin.getName(), -addGold);
				user1.sendUserUpdateMessage();
			}
			AdminManager.getInstance().addGold(id, addGold,admin.getId());
			int oldGold = user.userCurrency().get(CurrencyKind.Diamonds);
			user.userCurrency().add(CurrencyKind.Diamonds, addGold);
			LogUtils.addSpendLog(user, LogType.GMFK.getDesc() + admin1.getName(), addGold);
			user.sendUserUpdateMessage();
			req.getRequestDispatcher("/m_agentdes.jsp?id=" + id).forward(req, res);
			return;
		}

		// 减少金币
		else if ("reduceGold".equals(action)) {
			String id = req.getParameter("name");
			int addGold = PubFunc.parseInt(req.getParameter("gold"));
			if (addGold >= 0) {
				req.getRequestDispatcher("/m_info.jsp?info=7").forward(req, res);
				return;
			}
			User user = UserUtils.getUserByPrimaryKey(id);
			Admin admin1 = AdminManager.getInstance().getAdmin(id);
			if (user == null) {
				JSONObject js = new JSONObject("{'ok':1,'msg':'用户找不到'}");
				res.getWriter().print(js);
				return;
			}
			Admin admin = (Admin) req.getSession().getAttribute("admin");

			int group = admin.getGroup();
			admin = AdminManager.getInstance().getAdminById(admin.getId() + "");
			if (group == 1) {
				int oldGold = user.userCurrency().get(CurrencyKind.Diamonds);
				user.userCurrency().add(CurrencyKind.Diamonds, addGold);
				LogUtils.addSpendLog(user, LogType.GMFK.getDesc() + admin.getName(), addGold);
				int newGold = user.userCurrency().get(CurrencyKind.Diamonds);
				admin1.setGold(newGold);
				user.sendUserUpdateMessage();
				admin.saveDB();
				admin1.saveDB();
				admin.addSellerLog(new SellerLog(id, user.userExtra().getNickName(), oldGold, newGold));

				JSONObject js = new JSONObject("{'ok':1,'msg':'减少成功'}");
				res.getWriter().print(js);
				return;
			} else {
				JSONObject js = new JSONObject("{'ok':1,'msg':'减少失败'}");
				res.getWriter().print(js);
				return;
			}

		}
		// 充值
		else if ("sellerGold".equals(action)) {
			String id = req.getParameter("name");
			int addGold = PubFunc.parseInt(req.getParameter("gold"));
			if (addGold <= 0) {
				req.getRequestDispatcher("/m_info.jsp?info=5").forward(req, res);
				return;
			}
			User user = UserUtils.getUserByPrimaryKey(id);
			//Admin admin1 = AdminManager.getInstance().getAdmin(id);
			if (user == null) {
				JSONObject js = new JSONObject("{'ok':1,'msg':'用户找不到'}");
				res.getWriter().print(js);
				return;
			}
			Admin admin = (Admin) req.getSession().getAttribute("admin");
			int group = admin.getGroup();
			admin = AdminManager.getInstance().getAdminById(admin.getId() + "");
			if (admin == null || !admin.isFree()) {
				JSONObject js = new JSONObject("{'ok':0,'msg':'您的权限被删除,请联系我的工作人员'}");
				res.getWriter().print(js);
				return;
			}
			if (group == 1) {
				int oldGold = user.userCurrency().get(CurrencyKind.Diamonds);
				user.userCurrency().add(CurrencyKind.Diamonds, addGold);
				LogUtils.addSpendLog(user, LogType.GMFK.getDesc() + admin.getName(), addGold);
				int newGold = user.userCurrency().get(CurrencyKind.Diamonds);
				System.out.println(newGold);
				//admin1.setGold(newGold);
				user.sendUserUpdateMessage();
				//admin1.saveDB();
			} else {
				if (admin.getGold() < addGold) {
					JSONObject js = new JSONObject("{'ok':0,'msg':'您的房卡不足'}");
					res.getWriter().print(js);
					return;
				}
				int oldGold = user.userCurrency().get(CurrencyKind.Diamonds);
				user.userCurrency().add(CurrencyKind.Diamonds, addGold);
				LogUtils.addSpendLog(user, LogType.GMFK.getDesc() + admin.getName(), addGold);
				int newGold = user.userCurrency().get(CurrencyKind.Diamonds);
				user.sendUserUpdateMessage();
				//admin1.setGold(newGold);
				admin.setGold(admin.getGold() - addGold);
				admin.saveDB();
				//admin1.saveDB();
				admin.addSellerLog(new SellerLog(id, user.userExtra().getNickName(), oldGold, newGold));
			}
			JSONObject js = new JSONObject("{'ok':1,'msg':'成功'}");
			res.getWriter().print(js);
			return;
		} else if ("upgrade".equals(action)) {
			// int version = PubFunc.parseInt(req.getParameter("version"));
			// String iosUrl = req.getParameter("iosUrl");
			// int touristCode = PubFunc.parseInt(req.getParameter("touristCode"));
			// GameSetting.getInstance().setIosCode(version);
			// GameSetting.getInstance().setIosUrl(iosUrl);
			// GameSetting.getInstance().setTouristCode(touristCode);
			// GameSetting.getInstance().saveWx();
			req.getRequestDispatcher("/m_UpgradeInfo.jsp").forward(req, res);
			return;
		} else if ("sheyuan".equals(action)) {
			
			req.getRequestDispatcher("/m_UpgradeInfo.jsp").forward(req, res);
			return;
		} else if ("delagent".equals(action)) {
			String id = req.getParameter("id");
			AdminManager.getInstance().deleteAgent(id);
			req.getRequestDispatcher("/m_agent.jsp").forward(req, res);
			return;
		} else if ("changeagent".equals(action)) {
			String id = req.getParameter("id");
			AdminManager.getInstance().changeState(id);
			req.getRequestDispatcher("/m_agentdes.jsp?id=" + id).forward(req, res);
			return;
		} else if (action.equals("addnotice")) {
			String description = req.getParameter("des");
			if (!StringUtil.NullOrEmpty(description)) {
				Date startdate = new Date();
				Date enddate = TimeUtils.addDay(startdate, 100000);
				GlobNotice notice = new GlobNotice(description, startdate, enddate);
				NoticeManager.getInstance().addNewNotice(notice);
			}
			req.getRequestDispatcher("/m_noticeshow.jsp").forward(req, res);
			return;
		} else if (action.equals("deletenotice")) {
			String id = req.getParameter("id");
			NoticeManager.getInstance().removeNoticeFromDB(id);
			req.getRequestDispatcher("/m_noticeshow.jsp").forward(req, res);
			return;
		} else if ("changematch".equals(action)) {
			String id = req.getParameter("id");
			MatchSetting setting = MatchManager.getInstance().getMatchSetting(Integer.parseInt(id));
			setting.changeState();
			setting.save();
			if (setting.isOpen()) {
				MatchManager.getInstance().addMatch(setting.getId());
			} else {
				MatchManager.getInstance().closeMatch(setting.getId());
			}
			req.getRequestDispatcher("/m_matchlist.jsp").forward(req, res);
			return;
		}
		req.getRequestDispatcher("/m_agent.jsp").forward(req, res);
	}

}
